package com.kyz.launcherapp

import android.os.Bundle
import android.view.View
import androidx.core.app.ActivityCompat
import com.kyz.launcherapp.base.BaseSupportActivity
import com.kyz.launcherapp.fragments.MainFragment
import com.weikaiyun.fragmentation.ISupportFragment
import com.weikaiyun.fragmentation.SupportHelper

class MyLauncherActivity : BaseSupportActivity() {
    override fun getContentViewID(): Int = R.layout.activity_mylauncher

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        var mainFragment =
            SupportHelper.findFragment(supportFragmentManager, MainFragment::class.java)
        if (mainFragment == null) {
            mainFragment = MainFragment.newInstance()
            loadRootFragment(R.id.container, mainFragment)
        }
    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        setImmersiveFlags()
    }

    override fun onResume() {
        super.onResume()
        setImmersiveFlags()
    }

    private fun setImmersiveFlags() {
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_FULLSCREEN
                or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
    }

    override fun onBackPressedSupport() {
        val list = SupportHelper.getActiveFragments(
            supportFragmentManager
        )
        var fragmentNum = 0
        for (f in list) {
            if (f is ISupportFragment
                && (f as ISupportFragment).supportDelegate.isCanPop
                && (f as ISupportFragment).supportDelegate.isStartByFragmentation
            ) {
                fragmentNum++
            }
        }
        if (fragmentNum > 0) {
            pop()
        } else {
            //ActivityCompat.finishAfterTransition(mActivity)
        }
    }

}